package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class offerII_0044 {
    public List<Integer> largestValues(TreeNode root) {
        LinkedList<TreeNode> list=new LinkedList<>();
        List<Integer> result=new ArrayList<>();
        if(root==null) return result;
        int current=0;
        int number=1;
        int max=root.val;
        result.add(max);
        list.add(root);
        while(number>0){
            max= (int) Math.pow(2,31)+1;
            for(int i=0;i<number;++i){

                if(list.getFirst().left!=null){
                    list.add(list.getFirst().left);
                    current++;
                    max=Math.max(max,list.getFirst().left.val);

                }
                if(list.getFirst().right!=null){
                    list.add(list.getFirst().right);
                    current++;
                    max=Math.max(max,list.getFirst().right.val);
                }
                list.removeFirst();

            }
            number=current;
            if(current==0) break;
            current=0;
            result.add(max);

        }
        return result;
    }
}
